/**
 * Created by rong on 16/9/5.
 */
require.config(require_config);
define([
    'jquery',
    'common',
    'ready',
    'collection',
    'template',
    'model',
    'dialog',
    'datepicker'
], function ($, common, ready, Collection, template, Model, dialog, datepicker) {
    var index = function () {
        this.init();
    };
    index.prototype = {
        init: function () {
            loading();
            this.fetch();
            this.event();
        },
        fetch: function () {
            var _this = this;
            this.collection = new Collection({
                url: _global.url.api + 'admin/promotion_sign_up',
                condition: {},
                model: {
                    url: _global.url.api + 'admin/promotion_sign_up',
                    event: {
                        '.item-card:click': 'card',
                        '.item-edit:click': 'edit',
                        '.item-remove:click': 'remove',
                    },
                    card: function (e) {
                        _this.data = this.data;
                        var model = this;
                        e.preventDefault();
                        _this.dialog = dialog({
                            title: '制作卡片',
                            content: template('card-tpl', this.data),
                            width: 700,
                            ok: function () {
                                _this.data.type = 1;
                                _this.data.title = $('[name=title]').val();
                                _this.data.id = $('[name=id]').val();
                                _this.data.card_id = $('[name=card_id]').val();
                                if (_this.data.title == '') {
                                    Alert({msg: '请输入标题', type: 'error'});
                                    return false;
                                }
                                _this.data.intro = $('[name=intro]').val();
                                if (_this.data.intro == '') {
                                    Alert({msg: '请输入备注', type: 'error'});
                                    return false;
                                }
                                _this.data.corner_ico = $.trim($('[name="corner_ico"]:checked').val());
                                _this.data.tag_limit = $('[name="tag_limit"]:checked').val();
                                var user_tag_id_arr = new Array();
                                $('[name="user_tag_id"]:checked').each(function (i) {
                                    var user_tag_id = $.trim($(this).val());
                                    user_tag_id_arr.push(user_tag_id);
                                });
                                var card_cate = new Array();
                                $('[name="card_cate"]:checked').each(function (i) {
                                    var card_cate_id = $.trim($(this).val());
                                    card_cate.push(card_cate_id);
                                });
                                if (_this.data.tag_limit == 1) {
                                    if (user_tag_id_arr.length < 1) {
                                        Alert({msg: '请选择用户标签', type: 'error'});
                                        return false;
                                    }
                                }
                                _this.data.user_tag_id = user_tag_id_arr;
                                _this.data.card_cate = card_cate;

                                _this.data.url = $('[name=url]').val();
                                _this.data.content = $.trim($('[name=content]').val());
                                _this.data.image = $.trim($('[name=image]').val());
                                _this.data.width = $.trim($('[name=width]').val());
                                _this.data.height = $.trim($('[name=height]').val());
                                if (_this.data.width == '') {
                                    Alert({msg: '请输入宽度', type: 'error'});
                                    return false;
                                }
                                if (_this.data.height == '') {
                                    Alert({msg: '请输入高度', type: 'error'});
                                    return false;
                                }
                                if (_this.data.image == '') {
                                    Alert({msg: '请上传图片', type: 'error'});
                                    return false;
                                }
                                if (model.post()) {
                                    _this.collection.reload();
                                }
                                ;
                            }, cancel: function () {
                            }
                        });
                        _this.dialog.show();
                        $('.long_date').on('click', function () {
                            WdatePicker({
                                el: this.id,
                                dateFmt: 'yyyy-MM-dd HH:mm:ss'
                            });
                        });
                    },
                    edit: function (e) {
                        _this.data = this.data;
                        var model = this;
                        e.preventDefault();
                        _this.dialog = dialog({
                            title: '编辑卡片',
                            content: template('edit-tpl', this.data),
                            width: 700,
                            ok: function () {
                                _this.data.type = 1;
                                _this.data.title = $('[name=title]').val();
                                _this.data.id = $('[name=id]').val();
                                _this.data.card_id = $('[name=card_id]').val();
                                if (_this.data.title == '') {
                                    Alert({msg: '请输入标题', type: 'error'});
                                    return false;
                                }
                                _this.data.intro = $('[name=intro]').val();
                                if (_this.data.intro == '') {
                                    Alert({msg: '请输入备注', type: 'error'});
                                    return false;
                                }
                                _this.data.corner_ico = $.trim($('[name="corner_ico"]:checked').val());
                                _this.data.tag_limit = $('[name="tag_limit"]:checked').val();
                                var user_tag_id_arr = new Array();
                                $('[name="user_tag_id"]:checked').each(function (i) {
                                    var user_tag_id = $.trim($(this).val());
                                    user_tag_id_arr.push(user_tag_id);
                                });
                                var card_cate = new Array();
                                $('[name="card_cate"]:checked').each(function (i) {
                                    var card_cate_id = $.trim($(this).val());
                                    card_cate.push(card_cate_id);
                                });
                                if (_this.data.tag_limit == 1) {
                                    if (user_tag_id_arr.length < 1) {
                                        Alert({msg: '请选择用户标签', type: 'error'});
                                        return false;
                                    }
                                }
                                _this.data.user_tag_id = user_tag_id_arr;
                                _this.data.card_cate = card_cate;

                                _this.data.url = $('[name=url]').val();
                                _this.data.content = $.trim($('[name=content]').val());
                                _this.data.image = $.trim($('[name=image]').val());
                                _this.data.width = $.trim($('[name=width]').val());
                                _this.data.height = $.trim($('[name=height]').val());
                                if (_this.data.width == '') {
                                    Alert({msg: '请输入宽度', type: 'error'});
                                    return false;
                                }
                                if (_this.data.height == '') {
                                    Alert({msg: '请输入高度', type: 'error'});
                                    return false;
                                }
                                if (_this.data.image == '') {
                                    Alert({msg: '请上传图片', type: 'error'});
                                    return false;
                                }
                                if (model.put()) {
                                    _this.collection.reload();
                                }
                                ;
                            }, cancel: function () {
                            }
                        });
                        _this.dialog.show();
                        $('.long_date').on('click', function () {
                            WdatePicker({
                                el: this.id,
                                dateFmt: 'yyyy-MM-dd HH:mm:ss'
                            });
                        });
                    },
                    remove: function (e) {
                        e.preventDefault();
                        var model = this;
                        dialog({
                            'content': '确定删除吗?',
                            ok: function () {
                                if (model.del()) {
                                    _this.collection.reload();
                                }
                                ;
                            }, cancel: function () {
                            }
                        }).show(e.target);
                    },
                },
                callback: function () {
                    loaded();
                }
            });
            this.collection.fetch();
        },
        event: function () {
            this.data = {};
            var _this = this;
            $('#btn-search').on('click', function (e) {
                e.preventDefault();
                _this.collection.condition.type = $("#type").val();
                _this.collection.condition.start_time = $('input[name="start_time"]').val();
                _this.collection.condition.end_time = $('input[name="end_time"]').val();
                _this.collection.reload();
            });

            $('#btn-status').find('a').on('click', function (e) {
                e.preventDefault();
                $("#type").val('all');
                $('input[name="start_time"]').val('');
                $('input[name="end_time"]').val('');
                _this.collection.condition.type = $("#type").val();
                _this.collection.condition.start_time = $('input[name="start_time"]').val();
                _this.collection.condition.end_time = $('input[name="end_time"]').val();
                _this.collection.condition.status = $(e.target).attr('rel');
                _this.collection.reload();
                $(e.target).addClass('active').siblings().removeClass('active');
            });
            //保存
            $('#btn-add').on('click', function (e) {
                _this.data = {};
                e.preventDefault();
                _this.dialog = dialog({
                    title: '添加活动',
                    content: template('add-tpl', {}),
                    width: 700,
                    ok: function () {
                        _this.data.active_name = $('#active_name').val();
                        if (_this.data.active_name == '') {
                            Alert({msg: '请输入活动名称', type: 'error'});
                            return false;
                        }

                        _this.data.type = $("#type").val();
                        if (_this.data.type < 1) {
                            Alert({msg: '版块不能为空', type: 'error'});
                            return false;
                        }

                        _this.data.start_time = $('#dt_start').val();
                        _this.data.end_time = $('#dt_end').val();
                        if (_this.data.start_time != '' || _this.data.end_time != '') {
                            if (_this.data.start_time == '' || _this.data.end_time == '') {
                                Alert({msg: '开始时间或者结束时间不能为空', type: 'error'});
                                return false;
                            }
                            var date1 = new Date(Date.parse(_this.data.start_time));
                            var date2 = new Date(Date.parse(_this.data.end_time));
                            if (date1.getTime() > date2.getTime()) {
                                Alert({msg: '开始时间不能大于结束时间', type: 'error'});
                                return false;
                            }
                        }
                        _this.data.status = $("input[name='status']:checked").val();
                        if (new Model({
                                url: _global.url.api + 'promotion_add',
                                data: _this.data
                            }).post()) {
                            _this.collection.reload();
                        }
                    }, cancel: function () {
                    }
                });
                _this.dialog.show();
                $('.long_date').on('click', function () {
                    WdatePicker({
                        el: this.id,
                        dateFmt: 'yyyy-MM-dd HH:mm:ss'
                    });
                });
            });
            $('.long_date').on('click', function () {
                WdatePicker({
                    el: this.id,
                    dateFmt: 'yyyy-MM-dd HH:mm:ss'
                });
            });

            $('body').on('click', '[name="tag_limit"]', function (e) {
                var tag_limit = $('[name="tag_limit"]:checked').val();
                if (tag_limit == 1) {
                    $('.card_user_tag').show();
                } else {
                    $('.card_user_tag').hide();
                }
            });

            $('body').on('click', '#btn-upload', function (e) {
                _this.upload_dialog = dialog({
                    content: template('upload-tpl', {}),
                    width: 550,
                    ok: function () {
                        var img = $("#upload-frame")[0].contentWindow.image.get();
                        _this.imgPreview(img);
                    }, cancel: function () {
                    }
                });
                _this.upload_dialog.show();
            });

        },
        imgPreview: function (img) {
            if (typeof img.id !== 'undefined') {
                $('#img-view').html(template('img-tpl', img));
            }
        },
    };
    new index();
});